<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: PHP@妖孽 <dino_ma@163.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
/**
 * @todo iweb系统内部api
 * @since 2015-9-3
 */
class ServiceApiModel{
	public $error;//记录最后一次错误信息

	/**
	 * @todo 获取全部的名单列表
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function getServiceRollCallList(){
		$list = S('service_roll_call_list');
		if(!$list){
			$list = M('ServiceRollCall')->field('roll_call_id,roll_call_name')->where(array('status'=>1))->select();
			S('service_roll_call_list',$list,3600);
		}
		return $list;
	}

	/**
	 * @todo 名单中的员工列表
	 * @param int roll_call_id 名单id
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function getServiceRollCallMemberList($roll_call_id){
		static $list;
		$roll_call_id = (int)$roll_call_id;
		if($roll_call_id==0){
			$this->error = '名单id有误';
			return false;
		}
		if(is_null($list)){
			$data = S('service_roll_call_member_list');
		}
		if(isset($data[$roll_call_id])){
			$list = $data[$roll_call_id];
		}else{
			$list = M('ServiceRollCallMember')->where(array('roll_call_id'=>$roll_call_id))->field('roll_call_id,member_id')->select();
			if(empty($list)){
				return array();
			}
			S('service_roll_call_member_list',array($roll_call_id=>$list),3600);
		}
		return $list;
	}


	/**
	 * @todo 员工的名单列表
	 * @param int $member_id 名单id
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function getServiceMemberRollCallList($member_id){
		static $list;
		$member_id = (int)$member_id;
		if($member_id==0){
			$this->error = '用户id有误';
			return false;
		}
		if(is_null($list)){
			$data = S('service_member_roll_call_list');
		}
		if(isset($data[$member_id])){
			$list =$data[$member_id];
		}else{
			$list = M('ServiceRollCallMember')->field('roll_call_id,member_id')->where(array('member_id'=>$member_id))->select();
			if(empty($list)){
				return array();
			}
			S('service_member_roll_call_list',array($member_id=>$list),3600);
		}
		return $list;
	}


	/**
	 * @todo 名单中的用户列表
	 * @param int roll_call_id 名单id
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function getServiceRollCallCustomerList($roll_call_id){
		static $list;
		$roll_call_id = (int)$roll_call_id;
		if($roll_call_id==0){
			$this->error = '名单id有误';
			return false;
		}
		if(is_null($list)){
			$data = S('service_roll_call_customer_list');
		}
		if(isset($data[$roll_call_id])){
			$list = $data[$roll_call_id];
		}else{
			$list = M('ServiceRollCallCustomer')->where(array('roll_call_id'=>$roll_call_id))->field('customer_id')->select();
			if(empty($list)){
				return array();
			}
			S('service_roll_call_customer_list',array($roll_call_id=>$list),3600);
		}
		return $list;
	}


	/**
	 * @todo 活动下的销售网点
	 * @param int activity_id 活动id
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function getActivitySalePoint($activity_id){
		static $list;
		$activity_id = (int)$activity_id;
		if($activity_id==0){
			$this->error = '活动id有误';
			return false;
		}
		if(is_null($list)){
			$data = S('service_activity_sale_point_list');
		}
		if(isset($data[$activity_id])){
			$list = $data[$activity_id];
		}else {
			$pre = C('DB_PREFIX');
			$list = M()->table($pre.'service_activity_sale_point')->field("{$pre}service_sale_point.sale_point_id,{$pre}service_sale_point.sale_point_name")->where(array('activity_id'=>$activity_id,'status'=>1))->join("join {$pre}service_sale_point on {$pre}service_activity_sale_point.sale_point_id = {$pre}service_sale_point.sale_point_id")->select();
			if(empty($list)){
				return array();
			}
			S('service_activity_sale_point_list',array($activity_id=>$list));
		}
		return $list;
	}




	/**
	 * @todo 获取部门下的员工列表
	 * @param int department_id 部门id
	 * @author PHP@妖孽 <dino_ma@163.com>
	 * @author fang update
	 */
	public function getDepartmentMember($department_id){
		static $list;
	    $department_id = (int)$department_id;
	    if($department_id ==0){
	        $this->error = '部门id有问题~';
	        return false;
	    }
	    if(is_null($list)){
	    	$data = S('service_get_department_team_member_list');
	    }
	   	if(isset($data[$department_id])){
	   		$list = $data[$department_id];
	   	}else{
	   		$map = array();
	   		$map['department_id'] = $department_id;
	   		$pre = C('DB_PREFIX');
	   		$data = M()->table($pre.'auth_team_member')->field("{$pre}auth_team_member.member_id,{$pre}auth_team_member.team_id,{$pre}member.member_name,{$pre}member.is_full")->where($map)->join("join {$pre}member on {$pre}auth_team_member.member_id = {$pre}member.uid")->order('team_id asc')->select();
	   		$list = array();
	   		if(!empty($data)){
	   			for ($i=0;$i<count($data);$i++){
	   				$list[$data[$i]['team_id']]['team_id'] = $data[$i]['team_id'];
	   				$list[$data[$i]['team_id']]['_child'][] = $data[$i];
	   			}
	   			S('service_get_department_team_member_list',array($department_id=>$list));
	   		}else{
	   			return array();
	   		}
	   	}
	    return $list;
	}



	//部门下的组下的人员列表 (需要人员类型,人员名称) team_id;
	/**
	 * @todo 获取部门下的组下的员工列表(需要人员类型,员工名称) team_id
	 * @param int department_id 部门id
	 * @param int team_id	组id
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
// 	public function getDepartmentTeamMember($department_id,$team_id){
// 		$department_id = (int)$department_id;
// 		if($department_id ==0){
// 			$this->error = '部门id有问题~';
// 			return false;
// 		}
// 		$team_id = (int)$team_id;
// 		//缓存有问题所以暂时没加
// 		$data = S('service_get_department_team_member_list');
// 		$list = $data[$department_id][$team_id];
// 		if(false == $list){
// 			$map = array();
// 			$map['department_id'] = $department_id;
// 			if($team_id !=0){
// 				$map['team_id'] = $team_id;
// 			}
// 			$pre = C('DB_PREFIX');
// 			$list = M()->table($pre.'auth_team_member')->field("{$pre}auth_team_member.member_id,{$pre}member.member_name,{$pre}member.is_full")->where(array('department_id'=>$department_id,'team_id'=>$team_id))->join("join {$pre}member on {$pre}auth_team_member.member_id = {$pre}member.uid")->select();
// 			S('service_get_department_team_member_list',array($department_id=>array($team_id=>$list)));
// 		}
// // dump(array($department_id=>array($team_id=>$list)));die;
// 		return $list;
// 	}

	/**
	 * @todo 获取全部的装修需求tag
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function getAllDemand(){
		$data = S('service_all_demand');
		if(!$data){
			$demand_list = M('ServiceDemand')->where(array('status'=>1))->select();
			if(empty($demand_list)){
				$this->error = '暂无装修需求-联系马胜杰';
				return false;
			}
			$demand_ids = array_unique(array_column($demand_list,'demand_id'));
			if(empty($demand_ids)){
				$this->error = '暂无装修需求';
				return false;
			}
			$demand_content_list = M('ServiceDemandContent')->where(array('demand_id'=>array('in',$demand_ids),'status'=>1))->select();
			if(empty($demand_content_list)){
				$this->error = '暂无装修需求美容---联系马胜杰';
				return false;
			}
			$data = array();
			for ($i=0;$i<count($demand_list);$i++){
				$data[$demand_list[$i]['demand_id']]=$demand_list[$i];
			}
			for ($k=0;$k<count($demand_content_list);$k++){
				$data[$demand_content_list[$k]['demand_id']]['_child'][]=$demand_content_list[$k];
			}
			S('service_all_demand',$data);
		}
		return $data;

	}

	/**
	 * @todo 我的装修需求
	 * @param int customer_id 用户id
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function myDemand($customer_id){
		$customer_id = (int)$customer_id;
		if($customer_id ==0){
			$this->error = '用户id有误';
			return false;
		}
		static $list;
		if(is_null($list)){
			$data = S('service_my_demand');
		}
		if(!isset($data[$customer_id])){
			$list = M('ServiceCustomerDemand')->field('demand_content_id,demand_id')->where(array('customer_id'=>$customer_id))->select();
			if(empty($list)){
				return array();
			}
			S('service_my_demand',array($customer_id=>$list));
		}else{
			$list = $data[$customer_id];
		}
		return $list;
	}

	/**
	 * @todo 获取客服系统中的活动列表
	 * @param bool/int status 状态 (如果不传默认返回全部数据,) 0未推荐,1-推荐,2-已结束,3-回访中(创建回访任务时更新这个状态,且在生成回访任务中不在显示),4-回访任务结束
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function getServiceActivity($status=false){
		static $list;
		if(is_null($list)){
			$data = S('service_activity_list');
		}
		$map = array();//初始化查询条件
		if(false !== $status){
			//	$map['status'] = (int)$status;
			$map['status'] = array('in', (string)$status);//fang update
		}else{
			$status = 'all';
		}
		if(isset($data[$status])){
			$list = $data[$status];
		}else{
			$list = M('ServiceActivity')->where($map)->select();
			if(empty($list)){
				return array();
			}
			S('service_activity_list',array($status=>$list));
		}
		return $list;
	}


	/**
	 * @todo 获取用户在客服系统中的详细信息
	 * @param int customer_id 用户id
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function getServiceCustomerDetail($customer_id){
		static $list;
		$customer_id = (int)$customer_id;
		if($customer_id ==0){
			$this->error = '用户id有误';
			return false;
		}
		if(is_null($list)){
			$data = S('service_customer_detail');
		}
// 		if(!array_key_exists($customer_id, $data)){
// 			$list = M('ServiceCustomerDetail')->where(array('customer_id'=>$customer_id))->find();
// 			if($list['customer_name']==''){
// 				$list['customer_name']= getCustomerNameNew($list['customer_id']);
// 			}
// 			S('service_customer_detail',array($customer_id=>$list));
// 		}else{
// 			$list = $data[$customer_id];
// 		}
		
		if(!isset($data[$customer_id])){
			$list = M('ServiceCustomerDetail')->where(array('customer_id'=>$customer_id))->find();
			if(empty($list)){
				return array();
			}
			if($list['customer_name']==''){
				$list['customer_name']= getCustomerNameNew($list['customer_id']);
			}
			S('service_customer_detail',array($customer_id=>$list));
		}else {
			$list = $data[$customer_id];
		}
		return $list;
	}

	/**
	 * @todo 获取用户在客服系统中的房产信息(list)
	 * @param int customer_id 用户id
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function getServiceCustomerHouse($customer_id){
		static $list;
		$customer_id = (int)$customer_id;
		if($customer_id ==0){
			$this->error = '用户id有误';
			return false;
		}
		if(is_null($list)){
			$data = S('service_customer_house');
		}
		if(isset($data[$customer_id])){
			$list = $data[$customer_id];
		}else{
			$list = M('ServiceCustomerHouse')->where(array('customer_id'=>$customer_id,'status'=>1))->select();
			S('service_customer_house',array($customer_id=>$list));
		}
		return $list;
	}

	/**
	 * @todo 获取用户在客服系统中的某一个房产信息 array
	 * @param int house_id 房产id
	 * @param int customer_id 用户id
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function getServiceCustomerHouseDetail($customer_id,$house_id){
		static $list;
		$customer_id = (int)$customer_id;
		if($customer_id ==0){
			$this->error = '用户id有误';
			return false;
		}
		$house_id = (int)$house_id;
		if($house_id ==0){
			$this->error = '房产id有误';
			return false;
		}
		if(is_null($list)){
			$data = S('service_customer_house_detail');
		}
		if(isset($data[$house_id])){
			$list = $data[$house_id];
		}else{
			$list = M('ServiceCustomerHouse')->where(array('house_id'=>$house_id,'customer_id'=>$customer_id))->find();
			if(empty($list)){
				return array();
			}
			S('service_customer_house_detail',array($house_id=>$list));
		}
		return $list;
	}

	/**
	 * @todo 员工列表---需要修改
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
// 	public function getMemberList(){
// 		return M('Member')->where(array('status'=>1))->select();
// 	}


	/**
	 * @todo 某些人是否在这个部门中
	 * @param array
	 * <li>member_id</li> 员工id
	 * @param int department_id 	部门Id
	 * @author PHP@妖孽 <dino_ma@163.com>
	 * @author fang update
	 */
	public function memberIsInDepartment($member_id,$department_id){
	    if(empty($member_id) && !is_array($member_id)){//array
	        $this->error = 'member_id参数错误';
			return false;
	    }
	    if($department_id == 0){
	        $this->error = 'department_id参数错误';
	        return false;
	    }
	    $where['member_id'] = array('in', $member_id);
	    $where['department_id'] = $department_id;
	    $member_list = M('auth_team_member')->where($where)->select();
	    $return = array();
	    if($member_list){
	        $member_ids = array_column($member_list, 'member_id');
	        $return = array_intersect($member_ids, (array)$member_id);
	    }
	    return $return;

		/* if (M('auth_team_member')->where(array('member_id'=>$member_id,'department_id'=>$department_id))->find()){
			return true;
		}else{
			return false;
		} */

	}

	/**
	 * @todo 获取活动下的班车点列表（建议缓存叫service_activity_bus_list）
	 * @param int activity_id 活动id
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function getActivityBusList($activity_id){
		static $list;
		$activity_id = (int)$activity_id;
		if($activity_id==0){
			$this->error = '活动id有误';
			return false;
		}
		if(is_null($list)){
			$data = S('service_activity_bus_list');
		}
		if(isset($data[$activity_id])){
			$list = $data[$activity_id];
		}else{
			$map = array();
			$map['activity_id'] = $activity_id;
			$list =  M('ServiceActivityBus')->where($map)->select();
			if(empty($list)){
				return array();
			}
			S('service_activity_bus_list',array($activity_id=>$list));
		}
		return	$list;
	}

	/**
	 * @todo 获取活动下的短信模版列表（建议缓存叫service_activity_sms_list）
	 * @param int activity_id 活动id
	 */
	public function getActivitySmsTemplate($activity_id){
		static $list;
		$activity_id = (int)$activity_id;
		if($activity_id==0){
			$this->error = '活动id有误';
			return false;
		}
		if(empty($list)){
			$data = S('service_activity_sms_list');
		}
		if(isset($data[$activity_id])){
			$list = $data[$activity_id];
		}else {
			$map = array();
			$map['activity_id'] = $activity_id;
			$list =  M('ServiceActivitySms')->where($map)->select();
			if(empty($list)){
				return array();
			}
			S('service_activity_sms_list',array($activity_id=>$list));
		}
		return	$list;
	}

	/**
	 * @todo 获取活动下的发车时间
	 * @param int activity_id 活动id
	 */
	public function getActivityDepartureTime($activity_id){
		static $list;
		$activity_id = (int)$activity_id;
		if($activity_id==0){
			$this->error = '活动id有误';
			return false;
		}
		if(empty($list)){
			$data = S('service_activity_departure_time');
		}
		if(isset($data[$activity_id])){
			$list = $data[$activity_id];
		}else{
			$map = array();
			$map['activity_id'] = $activity_id;
			$list = M('ServiceActivityDepartureTime')->where($map)->select();
			if(empty($list)){
				return array();
			}
			S('service_activity_departure_time',array($activity_id=>$list));
		}
		return $list;
	}

	/**
	 * @todo 根据省市区读取销售网点
	 * @author sap
	 * @since 2015-9-5
	 */
	public function getAreaSalePointList($province_id=0,$city_id=0,$ward_id=0,$field= 'sale_point_id,sale_point_name'){
		static $list;
		//获取数据
		$province_id = (int)$province_id;
		$city_id = (int)$city_id;
		$ward_id = (int)$ward_id;
		if($province_id == 0 && $city_id == 0 && $ward_id == 0){
			$this->error = '参数丢失';
			return false;
		}
		if(empty($list)){
			$data =S('service_get_area_sale_point_list') ;
		}
		if(isset($data[$province_id.$city_id.$ward_id.$field])){
			$list = $data[$province_id.$city_id.$ward_id.$field];
		}else {
			$map = array();//查询条件初始化
			if($province_id != 0){
				$map['province_id'] = $province_id;
			}
			if($city_id != 0){
				$map['city_id'] = $city_id;
			}
			if($ward_id != 0){
				$map['ward_id'] = $ward_id;
			}
			$map['status'] = array('gt',-1);
			$list = M('ServiceSalePoint')->where($map)->field($field)->order('sale_point_id desc')->select();
			if(empty($list)){
				return array();
			}
			S('service_get_area_sale_point_list',array($province_id.$city_id.$ward_id.$field=>$list));
		}
		
		return $list;
	}

	/**
	 * @todo 根据省市区读取班车点
	 * @author sap
	 * @since 2015-9-5
	 */
	public function getAreaBusList($province_id=0,$city_id=0,$ward_id=0,$field= 'bus_point_id,bus_point_name'){
		static $list;
		//获取数据
		$province_id = (int)$province_id;
		$city_id = (int)$city_id;
		$ward_id = (int)$ward_id;
		if($province_id == 0 && $city_id == 0 && $ward_id == 0){
			$this->error = '参数丢失';
			return false;
		}
		if(is_null($list)){
			$data =S('service_get_area_bus_list');
		}
		if(isset($data[$province_id.$city_id.$ward_id.$field])){
			$list = $data[$province_id.$city_id.$ward_id.$field];
		}else{
			$map = array();//查询条件初始化
			if($province_id != 0){
				$map['province_id'] = $province_id;
			}
			if($city_id != 0){
				$map['city_id'] = $city_id;
			}
			if($ward_id != 0){
				$map['ward_id'] = $ward_id;
			}
			$map['status'] = array('gt',-1);
			$list =  M('ServiceBusPoint')->where($map)->field($field)->order('bus_point_id desc')->select();
			if(empty($list)){
				return array();
			}
			S('service_get_area_bus_list',array($province_id.$city_id.$ward_id.$field=>$list));
		}
		return $list;
	}

	/**
	 * @todo 获取这个员工下的活动下的人的快递
	 * @param int activity_id 活动id
	 * @param int member_id	员工id
	 * @param int customer_id 用户id
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function getMemberActivityExpressDetail($activity_id,$member_id,$customer_id){
		$activity_id = (int) $activity_id;
		$member_id = (int) $member_id;
		$customer_id = (int)$customer_id;
		$map =array();//初始化查询条件
		if($activity_id !=0){
			$map['activity_id'] = $activity_id;
		}
		if($member_id !=0){
			$map['member_id'] = $member_id;
		}
		if($customer_id !=0){
			$map['customer_id'] = $customer_id;
		}
		$list = M('ServiceExpressDetail')->where($map)->find();
		return $list;

	}
	//销售网点下的活动
		
	/**
	 * @todo 呼叫日志(不做任何判断,到时候谁往里写错了就干谁.~)
	 * @param int $customer_id 用户id
	 * @param int $call_type 0-呼出,1-呼入
	 * @param int $call_state 0-正常拨打;1-稍后拨打;2-无效
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function callLog($customer_id,$call_type,$call_state){
		M('service_call_log')->add(array('call_type'=>$call_type,'member_id'=>UID,'customer_id'=>$customer_id,'call_state'=>$call_state,'create_time'=>NOW_TIME));
	}
	
	/**
	 * @todo 行为日志(不做任何判断,到时候谁往里写错了就干谁.~)
	 * @param int $customer_id 用户id
	 * @param int $log_type 0-修改个人信息;1-修改装修需求;2-房产信息的增加;3-房产信息的删除;4-房产信息的编辑;5-活动的报名;6-发送短信;7-班车需求修改;8-Vip邮寄信息修改;9-活动的转化
	 * @param text $remark 操作了什么[XXXX]
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function operationLog($customer_id,$log_type,$remark){
		M('service_operation_log')->add(array('member_id'=>UID,'customer_id'=>$customer_id,'log_type'=>$log_type,'remark'=>$remark,'create_time'=>NOW_TIME));
	}
	
	/**
	 * @todo 返回最后一次错误信息
	 * @return string
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function getError(){
		return $this->error;
	}
	
	//--------------------------------商城用--------------------------------------
	
	/**
	 * @todo 返回所有商户信息
	 * @return array
	 * @author fzh <fanzhaohai@hrbjjhh.com>
	 */
	public function getMerchantList(){
	    //update by msj
	    $list = D('Merchant')->getMerchantList(array('status'=>1),$field= 'merchant_id,merchant_name','sort asc,merchant_id desc');
	    if(empty($list)){
	        return array();
	    }
	    return $list;
	}
	
	/**
	 * @todo 返回所有商户下的所有店铺
	 * @return array
	 * @author fzh <fanzhaohai@hrbjjhh.com>
	 */
	public function getShopList($merchant_id = 0){
	    $merchant_id = (int)$merchant_id;//update by msj
	    if($merchant_id ==0){
	        $this->erorr='参数错误～';
	        return false;
	    }
	    $map = array();
	    $map['status'] = 1;
	    $map['merchant_id'] = (int)$merchant_id;
	    $list = M('Shop')->order('shop_id desc, sort desc')->where($map)->field('shop_id,shop_name')->select();
	    if(empty($list)){
	        return array();
	    }
	    return $list;
	}
	
	/**
	 * @todo 店铺经营的一级品类
	 * @return array
	 * @param int|array $goods_category_id 为array时返回所有一级品类 为int时返回所有子类
	 * @author fzh <fanzhaohai@hrbjjhh.com>
	 */
	function getGoodsCategoryList($goods_category_id = array()){
	    //update by msj
	    if(is_array($goods_category_id) && !empty($goods_category_id)){
	        $map['goods_category_id'] = array('in',$goods_category_id);
	    }elseif (is_numeric($goods_category_id)){
	        $map['goods_category_pid'] = (int)$goods_category_id;
	    }else{
	        $this->error = '参数错误';
	        return false;
	    }
	    
	    $map['status'] = 1;
	    $field = 'goods_category_id,goods_category_name';
	    $order = 'goods_category_id desc, sort desc';
        $list = M('ShopGoodsCategory')->order($order)->where($map)->field($field)->select();
	    if(!empty($list)){
	        return $list;
	    }else{
	        return array();
	    }
	}
	
	/**
	 * @todo 所有全职员工列表
	 * @author fzh <fanzhaohai@hrbjjhh.com>
	 */
	public function getMemberList(){
	    //update by msj 
		return M('Member')->where(array('status'=>1,'is_full'=>0))->field('uid,member_name')->select();
	}
	
	/**
	 * @todo 品类详情
	 * @param int $goods_category_id 品类id
	 * @param string $field 查询字段
	 * @return false|array
	 * @author fzh
	 */
	public function goodsCategoryShow($goods_category_id=0,$field=''){
	    //获取数据
	    $goods_category_id = (int)$goods_category_id;
	    if($goods_category_id == 0){
	        $this->error = '请正确操作';
	        return false;
	    }
	    $map = array();//初始化查询条件
	    //查询条件
	    $map['goods_category_id'] = $goods_category_id;
	    $map['status'] = array('gt',-1);
	    //返回值
	    return M('ShopGoodsCategory')->where($map)->field($field)->find();
	}
	
	/**
	 * @todo 获取店铺品类
	 * @return array
	 * @author fzh <fanzhaohai@hrbjjhh.com>
	 */
	public function getShopCategory(){
	    $map['status'] = 1;
	    $order = 'sort desc,shop_category_id desc';
	    $field = 'shop_category_id,shop_category_name';
	    return M('shop_category')->order($order)->where($map)->field($field)->select();
	}
	
	/**
	 * @todo 获取商品列表
	 * @param array    $map where 条件
	 *                 如果需要某个位置的商品需要传入 字段position 类型 int 例如 $map['position'] = 1;
	 *                 如果需要跟个位置的商品需要传入 字段position 类型 array 例如  $map['position'] = array(1,2,3);
	 * @param string   $field 需要查询的字段
	 * @param int      $limit 查询数量
	 * @param string   $order 排序
	 * @author fanzhaohai
	 */
	public function getSaleGoodsNum($map, $field = true, $limit = 0, $order = 'sort desc, goods_id desc'){
		if(empty($map)){
			$map['status'] = 1;
		}
	    if(is_numeric($map['position'])){
	        $map[] = "position & {$map['position']} = {$map['position']}";
	        unset($map['position']);
	    }elseif(is_array($map['position'])){
	        $position = array_sum($map['position']);
	        $map[] = "position | {$position} = {$position}";
	        unset($map['position']);
	    }
		if($limit > 0){
			$goods_list = M('shop_goods')->where($map)->field($field)->order($order)->limit($limit)->select();
		}else{
			$goods_list = M('shop_goods')->where($map)->field($field)->order($order)->select();
		}
		return $goods_list;
	}
	
	/**
	 * @todo 获取商品列表
	 * @param array    $map where 条件
	 *                 如果需要某个位置的商品需要传入 字段position 类型 int 例如 $map['position'] = 1;
	 *                 如果需要跟个位置的商品需要传入 字段position 类型 array 例如  $map['position'] = array(1,2,3);
	 * @param int      $page 页码
	 * @param int      $page_count 每页条数
	 * @param string   $field 需要查询的字段
	 * @param string   $order 排序
	 * @author fang
	 */
	public function getSaleGoods($map, $page = 1, $page_count = 10, $field = true, $order = 'sort desc, goods_id desc'){
	    $page = (int)$page == 0 ? 1 : (int)$page;
	    if(empty($map)){
	        $map['status'] = 1;
	    }
	    if(is_numeric($map['position'])){
	        $map[] = "position & {$map['position']} = {$map['position']}";
	        unset($map['position']);
	    }elseif(is_array($map['position'])){
	        $position = '';
	        foreach ($map['position'] as $key => $val){
	            if($key>0) $position .= ' or ';
	            $position .= "position & {$val} = {$val}";
	        }
	        $map[] = $position;
// 	        $position = array_sum($map['position']);
// 	        $map[] = "{$position} ^ position = {$position}";
	        unset($map['position']);
	    }
	    $goods_list = M('shop_goods')->where($map)->field($field)->order($order)->page($page,$page_count)->select();
	    return ($goods_list)? $goods_list : null ;
	}
	
	/**
	 * @todo 获取商品促销信息
	 * @param array    $goods_ids 商品id
	 * @param string   $field  字段
	 * @param string   $order  排序 
	 * @author fang
	 */
	public function getSaleGoodsPromotion($goods_ids,$field = true , $order = 'promotion_id desc'){
		$goods_ids = (array)$goods_ids;
		$data = array();
		if(empty($goods_ids)){
		    $this->error = 'goods_ids参数错误';
		    return false;
		}
		$map['goods_id'] = array('in',$goods_ids);
		if($field === true){
     		$field = 'promotion_id,goods_id,promotion_price,promotion_stock,promotion_start_time,promotion_end_time,pic_id as promotion_pic_id';
		}
		$list = M('ShopGoodsPromotion')->where($map)->field($field)->order('promotion_id desc')->select();
		if(!empty($list)){
		    foreach ($list as $key=>$val){
		        //虚假时间临时用
// 		        $start_time = strtotime(date('Y-m-d').' 00:00:00');
// 		        $end_time = strtotime(date('Y-m-d').' 23:59:59');
// 		        $val['promotion_start_time'] = $val['promotion_start_time'] > $start_time ? $val['promotion_start_time'] : $start_time;
// 		        $val['promotion_end_time'] = $val['promotion_end_time'] < $end_time ? $val['promotion_end_time'] : $end_time;
		        $val['promotion_start_time'] = $val['promotion_start_time'];
		        $val['promotion_end_time'] = $val['promotion_end_time'];
		        $data[$val['goods_id']] = $val;
		    }
		}
		return $data;
	}
	
	/**
	 * @todo 获取商品标签信息
	 * @author fang
	 */
	public function getSaleGoodsTag($good_ids){
		if(empty($good_ids)){
			$this->error = 'goods_ids参数错误';
			return false;
		}
		$data = array();
		$good_tags = M('ShopGoodsTags')->where(array('goods_id'=>array('in',$good_ids)))->select();
	    //获取tag的名称
	    if($good_tags){
	        $tags_id_array = array_unique(array_column($good_tags, 'tags_id'));

	        $tags_list  = M('ShopTags')->field('tags_id,tags_name')->where(array('tags_id'=>array('in',$tags_id_array)))->select();
	        $tags_array = array_column($tags_list, 'tags_name', 'tags_id');
	        foreach ($good_tags as $key=>$val){
	        	if(isset($tags_array[$val['tags_id']])){
	        		$data[$val['goods_id']][] = $tags_array[$val['tags_id']];
	        	}
	        }
	        
	    }
		return $data;
	}
	
	/**
	 * @todo 获取商品详情
	 * @return array
	 * @author fzh <fanzhaohai@hrbjjhh.com>
	 */
	public function getGoodsShow($goods_id){
	    $goods_id = (int)$goods_id;
        if($goods_id == 0){
            return array();
        }
        $data = array();
	    //取得指定推荐商品
	    $map['goods_id'] = $goods_id;
	    $map['status'] = array('gt','0');
	    $goods_list = D('Admin/ShopGoods')->where($map)->field(true)->find();
	    if(empty($goods_list)){
	        return $data;
	    }else{
	        $data = $goods_list;
	    }
	    
	    //获取促销信息
	    if($data['goods_type'] == 1){
    	    $goods_promotion_list = $this->getSaleGoodsPromotion($goods_id);
    	    
    	    //获取促销缓存信息
	    	//$goods_promotion_list = getGoodsPromotionList($goods_id);
    	    if(isset($goods_promotion_list[$goods_id])){
    	        $data = array_merge($data,$goods_promotion_list[$goods_id]);
    	    }
	    }
	    return $data;
	}
	
	/**
	 * @todo 获取天天特卖的位置信息
	 * @return array
	 * @author fzh <fanzhaohai@hrbjjhh.com>
	 */
	function getSalePositionList($start_key,$end_key){
	    $start_key = (int)$start_key;
	    $end_key = (int)$end_key;
	    if($start_key==0 || $end_key==0){
	        $this->error = '获取位置出错';
	        return false;
	    }
       /* 读取数据库中的配置 */
       $config =   S('DB_CONFIG_DATA');
       if(!$config){
           $config =   api('Config/lists');
           S('DB_CONFIG_DATA',$config);
       }
	   $position = $config['GOODS_POSITION'];
	   $data = array();
	   if(empty($position)){
	       return $data;
	   }
	   foreach ($position as $key => $val){
	       if($key>=$start_key && $key<=$end_key){
	           $data[$key] = $val;
	       }
	   }
	   return $data;
	}
	
	/**
	 * @todo 正常商品的库存查询与设置
	 * @param $goods_id int 商品id
	 * @return false|int
	 * @author fzh <fanzhaohai@hrbjjhh.com>
	 */
	public function getShopGoodsStock($goods_id){
		$goods_id = (int)$goods_id;
		$num = (int)$num;
		if($goods_id==0){
			$this->error = '商品id错误';
			return false;
		}
		
		//查库存
		$data = S('shop_goods_stock_a_'.$goods_id);
		if(false === $data || is_null($data)){
		    $goods_info = M('shop_goods')->field('goods_stock')->where(array('goods_id'=>$goods_id))->find();
		    if(empty($goods_info)){
		        $this->error = '商品已售罄';
		        return false;
		    }
		    $data = $goods_info['goods_stock'];
		    S('shop_goods_stock_a_'.$goods_id,$data);
		}
		return $data;
	}
	
	/**
	 * @todo 促销商品的库存查询与设置
	 * @param $goods_id int 商品id
	 * @return false|int
	 * @author fzh <fanzhaohai@hrbjjhh.com>
	 */
	public function getShopGoodsPromotionStock($goods_id){
	    $goods_id = (int)$goods_id;
	    if($goods_id==0){
	        $this->error = '商品id错误';
	        return false;
	    }
	    //查库存
	    $data = S('shop_goods_promotion_stock_a_'.$goods_id);
	   
	    if(false === $data || is_null($data)){
	        $goods_promotion_info = M('shop_goods_promotion')->field('promotion_stock')->where(array('goods_id'=>$goods_id))->find();
	        if(empty($goods_promotion_info)){
	            $this->error = '促销商品信息错误';
	            return false;
	        }
	        $data = $goods_promotion_info['promotion_stock'];
	        S('shop_goods_promotion_stock_a_'.$goods_id,$data);
	    }
	    return $data;
	}
	
	/**
	 * @todo 获取店铺的详细信息
	 * @param int $shop_id 店铺id
	 * @author fzh
	 */
	function getShopInfo($shop_id){
	    $shop_id = (int)$shop_id;
	    if($shop_id == 0){
	        $this->error = '店铺id错误';
	        return false;
	    }
	    $map['shop_id'] = $shop_id;
	    $map['status'] = 1;
	    $list = M('shop')->where($map)->order('sort desc')->find();
	    return $list;
	}
	
	
	/**
	 * @todo  获取店铺list
	 * @param array    $map where 条件
	 * @param string   $field 需要查询的字段
	 * @param string   $order 排序
	 * @author fzh
	 */
	public function shopList($map, $field = true, $order = 'sort desc, shop_id desc'){
	    if(empty($map)){
	        $map['status'] = 1;
	    }
	    $shop_list = M('shop')->where($map)->field($field)->order($order)->select();
	    return ($shop_list)? $shop_list : null ;
	}
	
	
	//--------------------------广告-----------------------
	
	/**
	 * @todo 获取位置信息
	 * @param array $id 位置id
	 * @return  array
	 */
	function getAdPostion($id){
	    $id = (array)$id;
	    if(empty($id)){
	        $this->error = '位置id出错';
	        return false;
	    }
	    $list = S('ad_position_list');
	    if(empty($list)){
    	    $field = 'ad_position_id,ad_position_width,ad_position_height'
    	            .',ad_position_type,ad_position_limit,ad_position_template';
    	    $map['ad_position_id'] = array('in',$id);
    	    $map['status'] = array('gt',0);
    	    $ad_position_info = M('ad_position')->field($field)->where($map)->select();
    	    $ad_position_list = array();
    	    foreach ($ad_position_info as $val){
    	        $list[$val['ad_position_id']] = $val;
    	    }
	        S('ad_position_list',$list,3600);
	    }
	    $data = array();
	    foreach ($id as $val){
	        $data[$val] = $list[$val];
	    }
	    return $data;
	}
	
	/**
	 * @todo 获取广告信息
	 * @param array $id 位置id
	 * @return  array
	 */
	function getAdList($postion_id){
	    $data = array();
	    if(empty($postion_id) || !is_array($postion_id)){
	        $this->error = '广告id错误';
	        return false;
	    }
	    S('ad_list',null);
	    $list = S('ad_list');
	    if(empty($list)){
	       $map['status'] = 1;
	       $list = D('Admin/AdView')->field(true)->where($map)->order('sort desc, ad_id desc')->select();
	       foreach ($list as $val){
	           $data[$val['ad_position_id']][] = array(
	               'ad_name'=>$val['ad_name'],
	               'ad_link'=>$val['ad_link'],
	               'pic_id'=>$val['pic_id']
	           );
	       }
	       $list = $data;
	       S('ad_list',$list,3600);
	    }
	    return array_intersect_key($list,array_flip($postion_id));
	}
	
	
	//-------------------问答系统---------------
	
	/**
	 * @todo 文章分类
	 * @author fzh
	 * @param int ask_category_id 为0时返回全部数据 >0时返回单条数据
	 * @return array
	 */
	public function getAskCategory($ask_category_id = 0){
	    $ask_category_id = (int)$ask_category_id;
	    $data = array();
	    $list = S('ask_category_list');
	    if(empty($list)){
	        $map['status'] = 1;
	        $order = 'sort desc, ask_category_id desc';
	        $list = M('ask_category')->field(true)->where($map)->order($order)->select();
	        if(empty($list)){
	            return $data;
	        }
	        foreach ($list as $val){
	            $data[$val['ask_category_id']] = $val;
	        }
	        $list = $data;
	        S('ask_category_list',$list,3600);
	    }
	    return $ask_category_id>0 ? $list[$ask_category_id] : $list;
	}
	
	/**
	 * @todo 文章分类
	 * @author fzh
	 * @param int ask_category_id 为0时返回全部数据 >0时返回单条数据
	 * @return array
	 */
	public function getAskCategoryTree(){
	    S('ask_category_tree_list',null);
	    $list = S('ask_category_tree_list');
	    if(empty($list)){
            $list = toTree($this->getAskCategory(),'ask_category_id','ask_category_pid');
            S('ask_category_tree_list',$list,3600);
	    }
	    return $list;
	}
	/**
	 * @todo 获取分类标签
	 * @param array $category_id 分类id
	 * @author fzh
	 */
	public function getAskCategoryTags($category_id = array()){
	    $category_id = (array)$category_id;
	    $data= null;
	    if(empty($category_id)){
	        return $data;
	    }
	    //获取分类下的标签id集合
	    $map['ask_category_id'] = array('in',$category_id);
	    $category = M('ask_category_tags')->field('ask_category_id,ask_tags_id')->where($map)->select();
	    if(empty($category)){
	        return $data;
	    }
	    return $category;
	}
	
	/**
	 * @todo 获取标签
	 * @param array $map 
	 * @param int $limit 条数
	 * @author fzh
	 */
	public function getAskTags($map = array(),$limit = 0){
	    $map = (array)$map;
	    if(isset($map['position'])){
	        $map[] = "position & {$map['position']} = {$map['position']}";
	        unset($map['position']);
	    }
	    if($limit>0){
	        $list = M('ask_tags')->field(true)->where($map)->limit($limit)->select();
	    }else{
	        $list = M('ask_tags')->field(true)->where($map)->select();
	    }
        if(empty($list)){
            return array();
        }
        $list = array_column($list, 'ask_tags_name','ask_tags_id');
	    return $list;
	}
	
	
	/**
	 * @todo 获取标签详细
	 * @param int id 
	 * @author fzh
	 */
	public function getAskTagsInfo($id){
	    $id = (int)$id;
        if($id == 0){
            $this->error = '标签id错误';
        }
        $list = S('ask_tags_list');
        if(!isset($list[$id]) || empty($list[$id])){
            $map['ask_tags_id'] = $id;
            $info = M('ask_tags')->field(true)->where($map)->find();
            $list[$id] = $info;
            S('ask_tags_list',$list,3600);
        }
        return $list[$id];
	}
	
	/**
	 * @todo 获取文章列表
	 * @param array    $map where 条件
	 *                 如果需要某个位置的商品需要传入 字段position 类型 int 例如 $map['position'] = 1;
	 *                 如果需要跟个位置的商品需要传入 字段position 类型 array 例如  $map['position'] = array(1,2,3);
	 * @param int      $limit 取几条数据
	 * @param int      $page 页码
	 * @param int      $page_count 每页条数
	 * @param string   $field 需要查询的字段
	 * @param string   $order 排序
	 * @author fzh
	 */
	public function getArticleList($map, $limit = 0, $page = 1, $page_count = 10, $field = true, $order = 'sort desc, ask_article_id desc'){
	    $page = (int)$page == 0 ? 1 : (int)$page;
	    $page_count = (int)$page_count;
	    $limit = (int)$limit;
	    if(empty($map)){
	        $map['status'] = 1;
	    }
	    if(isset($map['position']) && is_numeric($map['position'])){
	        $map[] = "position & {$map['position']} = {$map['position']}";
	        unset($map['position']);
	    }elseif(isset($map['position']) && is_array($map['position'])){
	        $position = '';
	        foreach ($map['position'] as $key => $val){
	            if($key>0) $position .= ' or ';
	            $position .= "position & {$val} = {$val}";
	        }
	        $map[] = $position;
	        unset($map['position']);
	    }
	    if($limit>0){
            $article_list = M('ask_article')->where($map)->field($field)->order($order)->limit($limit)->select();
	    }else{
	        $article_list = M('ask_article')->where($map)->field($field)->order($order)->page($page,$page_count)->select();
	    }
        return $article_list;
	}
	
	/**
	 * @todo 获取指定位置的文章数据
	 * @param int $position 位置id
	 * @author fzh
	 */
	public function getPositionArticleList($position){
	    $map[] = "position & {$position} = {$position}";
	    $map['status'] = 1;
	    $order = 'sort desc, ask_article_id desc';
	    $article_list = M('ask_article')->where($map)->field(true)->order($order)->select();
	    return $article_list;
	}
	
	
	/**
	 * @todo 获取最新更新的文章
	 * @param int $limit 数量
	 * @author fzh
	 */
	public function getNewArticleList(){
	    $list = S('ask_article_new_list');
	    if(empty($list)){
	        $order = 'ask_article_id desc';
    	    $map['status'] = 1;
    	    $list = M('ask_article')->where($map)->field(true)->order($order)->limit(14)->select();
	        S('ask_article_new_list',$list,3600);
	    }
	    return $list;
	}
	
	/**
	 * @todo 获取文章详情
	 * @param int     $id  文章id
     * @return array|boolean
     * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function getArticleInfo($id){
	    static $list;
	    $id = (int)$id;
	    if($id == 0){
	        $this->error = '文章id错误';
	        return false;
	    }
        $list = S('ask_article_detail');
	    if(!isset($list[$id])){
	        $map['ask_article_id'] = $id;
        	$map['status'] = 1;
    	    $data = M('ask_article')->where($map)->find();
    	    if(empty($data)){
    	        return array();
    	    }
    	    
    	    $word_link = $this->getKeyWordLink();
    	    if(!empty($data['ask_article_content']) && !empty($word_link)){
    	       $find_arr = array_column($word_link, 'ask_keyword_name');
    	       foreach ($word_link as $val){
    	           $replace_arr[] = '<a href="'.$val['ask_keyword_link'].'">'.$val['ask_keyword_name'].'</a>';
    	       }
    	       $data['ask_article_content'] = str_replace($find_arr, $replace_arr, $data['ask_article_content']);
//     	       foreach ($word_link as $val){
//     	           if(!preg_match("/<a([^>]*)>(.*)".$val['ask_keyword_name']."(.*)<\/a>/", $data['ask_article_content'])){
//     	               $data['ask_article_content'] = preg_replace('/'.$val['ask_keyword_name'].'/',
//     	                   '<a href="'.$val['ask_keyword_link'].'">${0}</a>', $data['ask_article_content'],1);
//     	           }
//     	       }
    	    }
    	    $list[$id] = $data;
	        S('ask_article_detail',$list,3600);
	    }
	    return $list[$id];
	}
	
	/**
	 * @todo 文章锚点链接
	 * @author fzh
	 */
	public function getKeyWordLink(){
	    $list = S('ask_keyword_link_list');
	    if(empty($list)){
	        $order = 'ask_keyword_name desc, ask_keyword_id desc';
	        $map['status'] = 1;
	        $list = M('ask_keyword_link')->where($map)->field('ask_keyword_name,ask_keyword_link')->order($order)->select();
	        S('ask_keyword_link_list',$list,3600);
	    }
	    return $list;
	}
}

